package com.soft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.soft.entity.Class;
import com.soft.entity.Department;
import com.soft.entity.Student;
import com.soft.mapper.ClassMapper;
import com.soft.mapper.DepartmentMapper;
import com.soft.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassMapper classMapper;

    public List<Student> quallerAll(){
        List<Student>studentList=studentMapper.selectList(null);
        return studentList;
    }

    public int insert(String name,String age,String address,String class_id){
        Student student=new Student();
        student.setName(name);
        student.setAge(age);
        student.setAddress(address);
        student.setClass_id(class_id);
        int num= studentMapper.insert(student);
        return num;
    }

    public int updateNameById(int id,String name){
        UpdateWrapper<Student> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        Student student=studentMapper.selectOne(updateWrapper);
        student.setName(name);
        int num= studentMapper.updateById(student);
        return num;
    }

    public int delete(int id){
        int num= studentMapper.deleteById(id);
        return num;
    }

//根据某个学生的名字和指定班级名字，修改学生所属班级到指定班级

    public  int updateStudentClass(String name,String className){
        QueryWrapper<Student> studentWrapper=new QueryWrapper<>();
        studentWrapper.eq("name",name);
        Student student=studentMapper.selectOne(studentWrapper);
        QueryWrapper<Class> classWrapper=new QueryWrapper<>();
        classWrapper.eq("name",className);
        Class class1=classMapper.selectOne(classWrapper);
        String classId=class1.getId();
        student.setClass_id(classId);
        int num=studentMapper.updateById(student);
        return num;
    }



}
